package com.lkz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.Department;
import com.lkz.service.IDepartmentService;
import com.lkz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *  前端控制器
 *
 */
@RestController
@RequestMapping("/emp")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping("/dept/list")
    public List<Department> selectRole(){
        return departmentService.list();
    }
    @GetMapping("/dept")
    public Map getList(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Department> pages = new Page<Department>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Department> iPage = departmentService.selectList(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @GetMapping("/dept/selectByDel")
    public Map selectByDel(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Department> pages = new Page<Department>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Department> iPage = departmentService.selectByDel(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @DeleteMapping("/dept/del/{deptId}")
    public Map del(@PathVariable Integer deptId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",deptId);
        Department department = new Department();
        department.setIsDel(1);
        result.put("state",departmentService.update(department,queryWrapper));
        return result;
    }
    @DeleteMapping("/dept/recover/{deptId}")
    public Map recover(@PathVariable Integer deptId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",deptId);
        Department department = new Department();
        department.setIsDel(0);
        result.put("state",departmentService.update(department,queryWrapper));
        return result;
    }
    @PutMapping("/dept")
    public Map edit(Department department){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id",department.getDeptId());
        department.setUpdateTime(StringUtils.getNowTime());
        result.put("state",departmentService.update(department,queryWrapper));
        return result;
    }
    @PostMapping("/dept")
    public Map add(Department department){
        Map<String,Object> result = new HashMap<String,Object>();
        department.setCreateTime(StringUtils.getNowTime());
        department.setUpdateTime(department.getCreateTime());
        department.setIsDel(0);
        result.put("state",departmentService.save(department));
        return result;
    }
}
